package com.sweets.look.ui.page;

import android.content.Context;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import com.sweets.look.R;
import com.sweets.look.adapter.HistoryItem;
import com.sweets.look.database.DatabaseHandler;
import com.sweets.look.ui.browser.BookmarkManager;

public class PageManager {



    public static String getBookmarkPage(Context context, BookmarkManager bookmarkManager, String folder,
                                         int style) {

        StringBuilder stringBuilder = new StringBuilder();
        String v4 = "bookmarks.html";
        List<HistoryItem> v7;
        if (folder == null || (folder.equalsIgnoreCase(""))) {
            v7 = bookmarkManager.getAllBookmarks();
        } else {
            v7 = bookmarkManager.getBookmarksFromFolder(folder);
            v4 = "folder.html";
        }

        stringBuilder.append(WebPage.getWebHead(context.getString(R.string.action_bookmarks)));

        Iterator<HistoryItem> iterator = v7.iterator();
        switch (style) {
            case 2: {
                stringBuilder.append(WebPage.getWebStyle(WebPage.getWebCss(2))).append("<body>\n<div id=\"content\">\n");
                while (iterator.hasNext()) {
                    HistoryItem v5 = iterator.next();
                    stringBuilder.append(WebPage.getGridItem(v5.getUrl(), v5.getTitle()));
                }
                break;
            }
            case 3: {
                stringBuilder.append(WebPage.getWebStyle(WebPage.getWebCss(3)))
                        .append(WebPage.getScript("\nfunction init() {\nvar divs = document.getElementsByClassName(\"title\");\nfor(i=0;i<divs.length; i++){\ndivs[i].style.backgroundColor = '#009688';\n}\n}\nfunction getRandomColor(){\nreturn  \'#\' +\n(function(color){\nreturn (color +=  \'789abcdef789abcdef\'[Math.floor(Math.random()*16)])\n&& (color.length == 6) ?  color : arguments.callee(color);\n})(\'\');}"))
                        .append("<body onload=\"init()\">\n<div id=\"content\">\n");
                while (iterator.hasNext()) {
                    HistoryItem v5 = iterator.next();
                    stringBuilder.append(WebPage.getGridItem(v5.getUrl(), v5.getTitle()));
                }

                break;
            }
            default: {
                stringBuilder.append(WebPage.getWebStyle(WebPage.getWebCss(1))).append("<body>\n<div id=\"content\">\n");
                while (iterator.hasNext()) {
                    HistoryItem v5 = iterator.next();
                    stringBuilder.append(WebPage.getListItem(v5.getUrl(), v5.getTitle()));
                }
                break;
            }
        }

        stringBuilder.append("\n</div>\n</body>\n</html>");
        File f = new File(context.getFilesDir(), v4);
        try {
            FileWriter writer = new FileWriter(f, false);
            writer.write(stringBuilder.toString());
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "file://" + f;
    }

    public static String getHistoryPage(Context context) {
        StringBuilder v2 = new StringBuilder();
        v2.append(WebPage.getWebHead(context.getString(R.string.action_history)));
        v2.append(WebPage.getWebStyle(WebPage.getWebCss(1)));
        v2.append("<body>\n<div id=\"content\">\n");
        Iterator<HistoryItem> v6 = getWebHistory(context).iterator();
        while (v6.hasNext()) {
            HistoryItem v1 = v6.next();
            v2.append(WebPage.getListItem(v1.getUrl(), v1.getTitle()));
        }

        v2.append("\n</div>\n</body>\n</html>");
        File v4 = new File(context.getFilesDir(), "history.html");
        try {
            FileWriter v5 = new FileWriter(v4, false);
            v5.write(v2.toString());
            v5.close();
        } catch (IOException v0) {
            v0.printStackTrace();
        }

        return "file://" + v4;
    }

    public static String getHomePage(Context context, String searchText, boolean containLink,
                                     BookmarkManager bookmarkManager) {
        StringBuilder sb = new StringBuilder();
//      sb.append(WebPage.getWebHead(context.getString(R.string.home)));
        sb.append(WebPage.getWebHead("搜索或网址"));

/*      v3.append(WebPage.getWebStyle(WebPage.getWebCss(4)));
        v3.append("<body>\n<div id=\"content\">\n");
        v3.append(WebPage.getSearchPart(context.getString(R.string.search_hint), "file:///android_asset/logo.png"));
        if (containLink) {
            Iterator<HistoryItem> v5 = bookmarkManager.getWantedBookmarks().iterator();
            while (v5.hasNext()) {
                HistoryItem v2 = v5.next();
                v3.append(WebPage.getGridItem(v2.getUrl(), v2.getTitle()));
            }
        }*/

        sb.append(WebPage.getWebStyle(WebPage.getWebCss(3)))
                .append(WebPage.getScript("\nfunction init() {\nvar divs = document.getElementsByClassName(\"title\");\nfor(i=0;i<divs.length; i++){\ndivs[i].style.backgroundColor = getRandomColor();\n}\n}\nfunction getRandomColor(){\nreturn  \'#\' +\n(function(color){\nreturn (color +=  \'789abcdef789abcdef\'[Math.floor(Math.random()*16)])\n&& (color.length == 6) ?  color : arguments.callee(color);\n})(\'\');}"))
                .append("<body onload=\"init()\">\n<div id=\"content\">\n");

//      v3.append(WebPage.getScript(WebPage.getSearchJs(searchText)));
        sb.append("\n</div>\n</body>\n</html>");
        File file = new File(context.getFilesDir(), "homepage.html");
        try {
            FileWriter writer = new FileWriter(file, false);
            writer.write(sb.toString());
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "file://" + file;
    }

    private static List<HistoryItem> getWebHistory(Context context) {
        return new DatabaseHandler(context).getLastHundredItems();
    }
}

